<?php
namespace Admin\Controller;
use Think\Controller;
class CatalogController extends Controller {
	public function __construct(){
		parent::__construct();
		if(!$_SESSION['admin_is_login']){
			$this->error('还未登录，请登录',U('admin/auth/do_login'));
		}
	}
    public function index(){
    	$this->assign('level1',M('product_catalog')->where('parent_id=0')->select());
        $this->display();
    }
    public function add(){
    	$this->assign('level1',M('product_catalog')->where('parent_id=0')->select());
    	$this->display();
    }
    public function do_add(){
    	M('product_catalog')->add(array(
    		'name' => I('post.name'),
    		'parent_id' => I('post.parent_id')
    	));
    	$this->success('添加分类成功',U('admin/catalog/index'));
    }
    public function edit($id){
    	$this->assign('level1',M('product_catalog')->where('parent_id=0')->select());
    	$this->assign('catalogs',M('product_catalog')->where('id ='.$id)->find());
    	$this->display();
    }
    public function do_edit(){
    	$id = I('post.id');
    	M('product_catalog')->where('id ='.$id)->save(array(
    		'name' => I('post.name'),
    		'parent_id' => I('post.parent_id')
    	));
    	$this->success('编辑分类成功',U('admin/catalog/index'));
    } 
    public function delete($id){
    	M('product_catalog')->where('id ='.$id)->delete($id);
    	$this->success('删除分类成功',U('admin/catalog/index'));
    }
}